package com.fishingwithme.android.ui.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fishingwithme.android.data.model.CreditAction
import com.fishingwithme.android.data.repository.FishingRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

data class CreditActionUiState(
    val settings: CreditAction? = null,
    val isLoading: Boolean = true,
    val error: String? = null
)

@HiltViewModel
class CreditActionModel @Inject constructor(
    val repository: FishingRepository
) : ViewModel() {
    private val _uiState = MutableStateFlow(CreditActionUiState())

    val uiState: StateFlow<CreditActionUiState> = _uiState.asStateFlow()
    fun loadPointSettings() {
        viewModelScope.launch {
            _uiState.update { it.copy(isLoading = true, error = null) }
            try {
                val resultFlow =
                    repository.loadPointSettings();

                resultFlow.collect { result ->
                    result.fold(
                        onSuccess = { settings ->

                            _uiState.update {
                                it.copy(
                                    isLoading = false,
                                    settings = settings,
                                    error = null
                                )
                            }
                        },
                        onFailure = { throwable ->
                            _uiState.update {
                                it.copy(
                                    isLoading = false,
                                    error = throwable.message ?: "加载钓点详情失败"
                                )
                            }
                        }
                    )
                }
            } catch (e: Exception) {
                _uiState.update {
                    it.copy(
                        isLoading = false,
                        error = e.message ?: "未知错误"
                    )
                }
            }
        }
    }

    fun toggleAutoSkipAds() {
        viewModelScope.launch {
            _uiState.update { it.copy(isLoading = true, error = null) }
            try {
                val currentState = _uiState.value
                val currentAutoSkipAds = currentState.settings?.autoSkipAds
                val skipAdsEnabled = currentAutoSkipAds != 1
                repository.setAutoSkipAds(skipAdsEnabled)
                    .collect { result ->
                        result.fold(
                            onSuccess = {
                                _uiState.update {
                                    it.copy(
                                        isLoading = false,
                                        error = null
                                    )
                                }
                                uiState.value.settings?.autoSkipAds = if (skipAdsEnabled) 1 else 0;
                            },
                            onFailure = {
                                _uiState.update {
                                    it.copy(
                                        isLoading = false,
                                        error = "设置失败."
                                    )
                                }
                            }
                        )
                    }

            } catch (e: Exception) {
                _uiState.update {
                    it.copy(
                        isLoading = false,
                        error = e.message ?: "未知错误"
                    )
                }
            }
        }
    }
}
